<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>后台管理系统模板</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <meta name="keywords" content="模板">
    <meta name="description" content="后台管理系统的HTML模板。">
    <meta name="author" content="SoulCoder">
    <div th:include="~{views/common/include :: cssStyle }"></div>
    <!-- 引入bootstrapmarkdown样式-->
    <link rel="stylesheet" th:href="@{/static/js/plugins/markdown/css/bootstrap-markdown.min.css}">
</head>
<body>
<div class="coder-layout-web">
    <div class="coder-layout-container">
        <!--左侧导航-->
        <div th:include="~{views/common/include :: #asideStyle }"></div>
        <!--End 左侧导航-->
        <!--头部信息-->
        <div th:include="~{views/common/include :: #headerStyle }"></div>
        <!--End 头部信息-->
        <!--页面主要内容-->
        <main class="coder-layout-content">

            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <!-- 查询搜索框start-->
                                <div id="toolbar">
                                    <div class="my-container">
                                        <label class="myLabel-content">名称：</label>
                                        <div class="myText-content">
                                            <input id="q_name" name="name" type="text" class="form-control" placeholder="输入角色名称">
                                        </div>
                                    </div>

                                    <div class="myBtn-content">
                                        <button id="search" type="button" class="btn btn-primary" data-toggle="modal" data-method="search">
                                            <i class="mdi mdi-account-search"></i>搜索</button>
                                    </div>
                                </div>
                                <!-- 查询搜索框end-->
                                <!-- 按钮区start-->
                                <div class="toolbar-btn-action">
                                    <button sec:authorize= "hasRole('管理员')" type="button" class="btn btn-primary" data-toggle="modal" data-method="add" >
                                        <i class="mdi mdi-plus"></i>新增
                                    </button>
                                    <button sec:authorize= "hasRole('管理员')" type="button" class="btn btn-pink" data-toggle="modal" data-method="setRolePermission" >
                                        <i class="mdi mdi-settings-box"></i>设置权限
                                    </button>
                                </div>
                                <!-- 按钮区end-->
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table id="roleTable" ></table>
                                </div>
                            </div>

                            <!-- 新增表单start-->
                            <div class="modal fade" id="roleAddModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" id="exampleModalLabel">新增角色</h4>
                                        </div>
                                        <form id="roleAddForm" method="post" enctype="multipart/form-data">
                                            <div class="modal-body">
                                                <!-- 角色名称-->
                                                <div class="form-group">
                                                    <label for="a_name" class="control-label">角色名称：</label>
                                                    <input type="text" class="form-control" name="name" id="a_name">
                                                </div>
                                                <!-- 角色描述-->
                                                <div class="form-group">
                                                    <label  class="control-label">角色描述：</label>
                                                    <textarea id="editor"   type="text" name="desc" data-provide="markdown" rows="10"></textarea>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                <button type="button" data-method='save' class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <!-- 新增表单结束-->

                            <!-- 修改表单start-->
                            <div class="modal fade" id="roleEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" >修改角色</h4>
                                        </div>
                                        <form id="roleEditForm" method="post" enctype="multipart/form-data">
                                            <div class="modal-body">
                                                <input type="hidden" id="e_id" class="form-control" name="id">
                                                <div class="form-group">
                                                    <label class="control-label">角色名称：</label>
                                                    <input type="text" id="e_name" class="form-control" name="name">
                                                </div>

                                                <!-- 角色描述-->
                                                <div class="form-group">
                                                    <label  class="control-label">角色描述：</label>
                                                    <textarea id="e_editor"   type="text" class="form-control"
                                                              name="desc" data-provide="markdown" rows="10"></textarea>
                                                </div>


                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                <button type="button" data-method='editSave' class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <!-- 修改表单end-->

                            <!-- 设定角色start-->
                            <div class="modal fade" id="setRolePermissionModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" >添加角色对应的权限</h4>
                                        </div>
                                        <form id="addRolePermissionForm" method="post" enctype="multipart/form-data">
                                            <div class="modal-body">
                                                <input type="hidden" id="rp_roleId" class="form-control" name="id">
                                                <div class="form-group">
                                                    <label  class="control-label">角色名称：</label>
                                                    <input type="text" id="rp_name" class="form-control" name="name">
                                                </div>
                                                <!-- 角色列表-->
                                                <div class="form-group row">
                                                    <div class="col-xs-12">
                                                        <label  class="control-label">选择权限：</label>
                                                    </div>
                                                    <div class="col-xs-12">
                                                        <label th:each="permisison : ${permissions}" style="margin-right: 10px">
                                                            <input th:id="'pid_'+${permisison.id}" name="permissions[]" type="checkbox" class="checkbox-child"  th:value="${permisison.id}">
                                                            <span th:text="${permisison.title}"></span>
                                                        </label>

                                                    </div>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                                <button type="button" data-method='saveRolePermission' class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <!-- 设定角色end-->


                    </div>
                </div>
            </div>
    </div>

    </main>

</div>
</div>

<div th:include="~{views/common/include :: #jsStyle}"></div>
<script type="text/javascript"  th:src="@{/static/js/jquery.serialize-object.min.js}"></script>
<script  th:src="@{/static/js/plugins/markdown/js/markdown.js}"></script>
<script  th:src="@{/static/js/plugins/markdown/js/to-markdown.js}"></script>
<script  th:src="@{/static/js/plugins/markdown/js/bootstrap-markdown.js}"></script>
<script  th:src="@{/static/js/plugins/markdown/js/marked.js}"></script>

<!--图表插件-->
<script type="text/javascript">

    $(document).ready(function(e){
        $('#roleTable').bootstrapTable({
            url: '/role/listpage',                      //请求后台的URL（*）
            method: 'GET',                      //请求方式（*）
            //toolbar: '#toolbar',              //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: true,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
            pageSize: 10,                     //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            iconsPrefix: 'mdi', // 自定义表格右上角的图标，这个必须要定义，因为不是官方模板，icon图标样式名字不同会导致图标无法显示
            icons:  {
                refresh: 'mdi-refresh',              // 刷新图标
                columns: 'mdi-format-list-bulleted', // 列图标
                toggleOff: 'mdi-toggle-switch-off',  // 切换光
                toggleOn: 'mdi-toggle-switch',       // 切换开
                detailOpen: 'mdi-plus',              // 详情开
                detailClose: 'mdi-minus',            // 详情光
                fullscreen: 'mdi-fullscreen'         // 全屏图标
            },
            search: false,                      //是否显示表格搜索
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列（选择显示的列）
            showRefresh: true,                  //是否显示刷新按钮
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            //height: 500,                      //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
            cardView: false,                    //是否显示详细视图
            detailView: false,                  //是否显示父子表
            singleSelect: false, //开启单选,想要获取被选中的行数据必须要有该参数
            queryParams : function(params) {//上传服务器的参数
                var temp = {
                    offset :params.offset + 0,// SQL语句起始索引
                    page : params.limit,  // 每页显示数量
                    name:params.name
                };
                return temp;
            },
            columns: [
                {
                    align : 'center',
                    checkbox: true
                },{
                    field: 'id',
                    title: '角色编号'
                }, {
                    field: 'name',
                    title: '角色名称',
                    width: '120px'
                },
                {
                    field: 'permissions',
                    title: '所属权限',
                    formatter:permissionsFormatter
                },
                {
                    field: 'doOpt',
                    title: '操作',
                    width: '200px',
                    formatter : optFormatter
                }]
        });

        function permissionsFormatter(value,row, index){
            if(row.permissions.length >0){
                var roleStr = "";
                for(var i=0;i<row.permissions.length;i++){
                     roleStr += "<span  class='label label-warning'>"+row.permissions[i].title+"</span>"

                }

                return roleStr;
            }else{
                return "<span class='label label-danger'>未分配权限</span>"
            }

        }
        function optFormatter(value,row, index){
            var c = '<a class="btn btn-xs btn-default" href="#!"  onclick=\'edit("' + row.id + '")\' title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>';
            var e = '<div class="btn btn-xs btn-default"  href="#!" onclick="del(\''+row.id+'\')" title="删除"  data-toggle="tooltip"><i class="mdi mdi-window-close"></i><div/> ';
            return c + e ;
        }

        //绑定事件
        $("button,a").on('click',function(){
            //获取到 a标签里面配置 data-method
            var methodName = $(this).data('method');
            if(methodName){
                doMethod[methodName]();
            }
        });
        //新增markdown变量
        var markDownObj = null;

        var doMethod = {
            search: function () {
                let name = $("#q_name").val();
                var queryParam = {
                    query: {
                        name: name
                    }
                }
                $('#roleTable').bootstrapTable('refresh', queryParam)
            },
            add: function () {
                //弹出新增角色的对话框
                $("#roleAddModal").modal({show: true})
                markDownObj = $("#editor").markdown()
            },
            save: function () {
                let roleName = $("#a_name").val();
                let roleDesc = marked(markDownObj[0].value.trim());

                $.ajax({
                    url: '/role/addRole',
                    type: 'POST',
                    data: {"name":roleName,"desc":roleDesc},
                    success: function (data) {
                        if (data.isSuccess) {
                            $("#roleAddModal").modal('hide');
                            $("#roleTable").bootstrapTable('refresh');
                        }
                    }
                })


            },
            editSave:function(){
                let roleId = $("#e_id").val();
                let roleName = $("#e_name").val();
                let roleDesc = marked(edit_markDownObj[0].value.trim());

                $.ajax({
                    url: '/role/editRole',
                    type: 'POST',
                    data: {"id":roleId,"name":roleName,"desc":roleDesc},
                    success: function (data) {
                        if (data.isSuccess) {
                            $("#roleEditModal").modal('hide');
                            $("#roleTable").bootstrapTable('refresh');
                        }
                    }
                })

            },
            setRolePermission:function(){
                //判断用户是否选中的数据，如果没有选中 给予提示
                let rows =  $("#roleTable").bootstrapTable('getSelections');
                if(rows.length <= 0 ){
                    $.confirm({
                        title:'温馨提示',
                        content:'亲 请选中一行数据进行操作',
                        type:'green',
                        buttons:{
                            ok:{
                                text:'谢谢',
                                btnClass:'btn-green'
                            }
                        }
                    })
                    return;
                }

                //弹出设置权限对话框
                $("#setRolePermissionModal").modal({show:true})
                //回显数据操作
                $("#rp_roleId").val(rows[0].id)
                $("#rp_name").val(rows[0].name)
                 let permissions = rows[0].permissions
                //先情况所有的多选框数据
                $("input[name='permissions[]']").prop('checked',false)
                //设置权限
                permissions.forEach((permission,index)=>{
                    var pid = permission.id;
                    $("#pid_"+pid).prop('checked',true)
                })

            },
            saveRolePermission:function(){
                //保存角色权限
                //获取表单数据
                var rpParamObj = $("#addRolePermissionForm").serializeObject()
                var sendParam = {
                     "roleId":rpParamObj.id,
                     "permissionIds":rpParamObj.permissions
                }
                $.ajax({
                    type:'POST',
                    url:'/role/saveRolePermission',
                    dataType:'json',
                    data:JSON.stringify(sendParam),
                    contentType:'application/json;charset=utf-8',
                    success:function(data){
                        if(data.isSuccess){
                            $.confirm({
                                title:'温馨提示',
                                content:'保存成功',
                                type:'green',
                                buttons:{
                                    ok:{
                                        text:'谢谢',
                                        btnClass:'btn-green'
                                    }
                                }
                            })
                            $("#setRolePermissionModal").modal('hide')
                            $("#roleTable").bootstrapTable('refresh')
                        }else{
                            $.confirm({
                                title:'温馨提示',
                                content:'操作失败'+data.message,
                                type:'red',
                                buttons:{
                                    ok:{
                                        text:'谢谢',
                                        btnClass:'btn-red'
                                    }
                                }
                            })
                        }
                    }
                })
            }
        }


    });

    //修改markdown变量
    var edit_markDownObj=null;
    //表格修改方法 --弹出一个修改对话框并且做回显
    function edit(id){
        //弹出修改对话框
        $("#roleEditModal").modal({show:true})
        edit_markDownObj=$("#e_editor").markdown()
        //根据id得到行数据
        var editRow = $("#roleTable").bootstrapTable('getRowByUniqueId',id)
        $("#e_id").val(editRow.id)
        $("#e_name").val(editRow.name)
        $("#e_editor").val(editRow.desc)

    }
    function del(id){

        $.confirm({
            title:'温馨提示',
            content:'亲 你真的忍心删除吗？',
            type:'green',
            buttons:{
                ok:{
                    text:'确定',
                    btnClass:'btn-green',
                    action:function(){

                        //根据id 删除数据
                        $.get("/role/deleteRole",{"id":id},function(data){
                            if(data.isSuccess){
                                $.confirm({
                                    title:'温馨提示',
                                    content:'删除成功',
                                    type:'green',
                                    buttons:{
                                        omg:{
                                            text:'谢谢',
                                            btnClass:'btn-green'
                                        }
                                    }
                                })
                                //刷新数据
                                $("#roleTable").bootstrapTable('refresh');
                            }else{
                                $.confirm({
                                    title:'温馨提示',
                                    content:'删除失败'+data.message,
                                    type:'red',
                                    buttons:{
                                        omg:{
                                            text:'关闭',
                                            btnClass:'btn-red'
                                        }
                                    }
                                })
                            }
                        })
                    }
                },
                cancel:{
                    text:'取消',
                    btnClass:'btn-red'
                }
            }
        })


    }




</script>


    <style>
      .md-header{
          margin-left: 0px;
      }
    .my-container {
        float: left;
        display: inline-block;
        margin-right:30px;
    }

    .myLabel-content ,.myText-content,.myBtn-content{
        float: left;
        display: inline-block;
        line-height: 40px;
        margin-left: 10px;
    }
    .myLabel-content,.myText-content input[type='text'],.myBtn-content .btn {
        height: 38px;
        font-size: 15px;
    }
    .myBtn-content .btn {
        margin-bottom: 10px;
        margin-right: 20px;
    }
</style>




</body>
</html>
